package com.skyolder.milkbee.web.rest.my;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Card;
import com.skyolder.milkbee.domain.Follow;
import com.skyolder.milkbee.domain.User;
import com.skyolder.milkbee.service.CardService;
import com.skyolder.milkbee.service.FollowService;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;
import com.skyolder.milkbee.web.util.WebUserUtil;

import io.swagger.annotations.ApiParam;

@RestController("myFocusCardResource")
@RequestMapping("/api/my")
public class FocusCardResource {
	@Inject
	private WebUserUtil webUserUtil;
	@Inject
	private CardService cardService;
	@Inject
	private FollowService followService;

	@GetMapping("/focuscard")
	@Timed
	public ResponseEntity<List<Card>> getMyFocuscards(@ApiParam Pageable pageable) throws URISyntaxException {
		User user = webUserUtil.getCurrentUser();
		List<Follow> follows = followService.findMyFocus(user.getId());
		List<Long> them = new ArrayList<>();
		follows.forEach(follow -> {
			them.add(follow.getToUserId());
		});
		Page<Card> page = cardService.findByUserIdIn(them, pageable);
		HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/my/focuscard");
		return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
	}

}
